define(function(){
	// checkbox
	var FormCheck= {
		valueCheck:function(checkConfig){
			var errorResult={
				'status':false,
				'result':''
			}
			switch (checkConfig.type) {
				case 'length':
					var minLen=1;
					if(checkConfig.minLen!==undefined){
						minLen=checkConfig.minLen;
					}
					var maxLen=checkConfig.maxLen || 0;
					//isBlur=true;
					var resultStr='';
					if(maxLen>0){
						resultStr=' %1 - %2 characters,'.tr('formCheck').arg(minLen,maxLen);
					}
					if (checkConfig.value.length< minLen) {
						if(minLen==1){
							errorResult.result=resultStr+'Cannot be empty'.tr('formCheck');
						}else{
							errorResult.result=resultStr+('must be %1 digits or longer').tr('formCheck').arg(minLen);
						}
					}else if(maxLen >0 && checkConfig.value.length > maxLen){
						errorResult.result=resultStr+('must be %1 digits or shorter').tr('formCheck').arg(maxLen);
					}else{
						errorResult.status=true;
					}

					break;
				case 'passwordleval':
					var pl_0=0,pl_A=0,pl_a=0,pl_$=0,pl_l=0;
					function CharMode(pwdLetter){
					    if( pwdLetter >= 48 && pwdLetter <= 57 ){//数字
					        pl_0=1;
					    }else if( pwdLetter >= 65 && pwdLetter <= 90 ){//大写字母
					        pl_A=1;
					    }else if( pwdLetter >= 97 && pwdLetter <= 122 ){//小写字母
					        pl_a=1;
					    }else{//特殊字符
					        pl_$=1;
					    }
					}
					$.each(checkConfig.value.split(''),function(i,l){
						CharMode(l.charCodeAt(0));
					})
				    if( checkConfig.value.length > 4 ){
				    	pl_l=pl_0+pl_A+pl_a+pl_$;
						errorResult.status=true;
						errorResult.result=pl_l;
				    }

					break;
				case 'cn':
					if (!checkConfig.value.match(/^[\u4e00-\u9fa5]{1,10}$/)) {
						errorResult.result = '内容必须为中文';
					}else{
						errorResult.status=true;
					}
					break;
				case 'uname':
					if ($.trim(checkConfig.value)!='' && !checkConfig.value.substr(0,1).match(/^(\w|[\u4E00-\u9FA5])*$/)) {
						errorResult.result = '首字母必须为中英文或数字';
					}else{
						errorResult.status=true;
					}
					break;
				case 'filename':
					if(!!/[\\\/:\*\?"<>\|]/gi.test(checkConfig.value)){
						errorResult.result = ('文件名不能包含\\\/:\*\?"<>\|').tr('formCheck');
					}else{
						errorResult.status=true;
					}

					break;
				case 'phone':
					var phoneNumber=$.parseInt(checkConfig.value);
					if(phoneNumber<10000000000 || phoneNumber>=30000000000 ){
						errorResult.result = 'Mobile number is wrong'.tr('formCheck');
					}else{
						errorResult.status=true;
					}
					break;
				case 'number':
					var value = $.parseInt(checkConfig.value);
					if(!value) {
						errorResult.result='Must be numeric'.tr('formCheck');
					} else {
						errorResult.status=true;
					}

					break;
				case 'justnumber':
					var value = !isNaN(checkConfig.value);
					if(!value) {
						errorResult.result='只能是数字'.tr('formCheck');
					} else {
						errorResult.status=true;
					}
					break;
				case 'email':
					if (!checkConfig.value.match(/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/)) {
						errorResult.result = 'Email is wrong'.tr('formCheck');
					}else{
						errorResult.status=true;
					}
					break;
				case 'idcard':
					if(!checkConfig.value.match(/^\d{17}(\d|X)$/i)){
						errorResult.result = 'Identification number is wrong'.tr('formCheck');
					}else{
						errorResult.status=true;
					}
					break;
				default:
					errorResult.result = 'noThisType';
					break;
			}
			return errorResult;
		},
		inputCheck:function ($input,isBlur){
			var checkTypeAttr=$input.attr('data-formcheck');
			if(checkTypeAttr===undefined){
				return false;
			}
			var checkTypeArr=checkTypeAttr.split(',');
			var checkValue=$input.value();
			//清除两端空白
			/*if($input.attr('type')!='password' && isBlur){
				checkValue=$.trim(checkValue);
				$input.val(checkValue);
			}*/
			var checkResult;
			$.each(checkTypeArr, function(i,checkTypeName) {
				var checkConfig={
					value:checkValue,
					type:checkTypeName
				};
				switch(checkTypeName){
					case 'length':
						if($input.attr('data-minlen')){
							checkConfig.minLen=$.parseInt($input.attr('data-minlen'));
						}
						checkConfig.maxLen=$.parseInt($input.attr('data-maxlen'));
						break;
				}
				checkResult=FormCheck.valueCheck(checkConfig);
				if(checkResult.status===false){
					return false;
				}
			});
			//解析结果
			checkResult.followTips=true;
			FormCheck.checkTips($input,checkResult);
			return checkResult;
		},
		checkTips:function ($input,checkResult){
			// console.log('checkResult:',checkResult)
			function tipsPosition(){

			}
			function useFollowTips(){
				if($input.nextAll('.inputErrorTips').length<1){
					$input.after('<div class="inputErrorTips"></div>');
				}
				var $inputErrorTips=$input.nextAll('.inputErrorTips').eq(0);
				if(checkResult.status){
					$inputErrorTips.html('').hide();
				}else{
					$inputErrorTips.html(checkResult.result);
					var tipsVisible=$inputErrorTips.is(':visible');
					if(tipsVisible){
						$inputErrorTips.stop(false,false).fadeOut(200,function(){
							$inputErrorTips.fadeIn(200);
						});
					}else{
						$inputErrorTips.show();
					}
					var tipsMarginLeft=$input[0].getBoundingClientRect().left-$inputErrorTips[0].getBoundingClientRect().left;
					if(tipsMarginLeft!=0){
						$inputErrorTips.css('margin-left',tipsMarginLeft);
					}


				}
			}
			function usePopUpBoxTips(){
				if(checkResult.status===false){
					new PopUpBox({
						'type':'menu',
						'style':'arrowDown black_alpha80',
						'show':true,
						'content':checkResult.result,
						'focus':false,
						'follow':{
							$dom:$input,
							direction:'center top'
						}
					});
				}
			}
			if(checkResult.followTips){
				useFollowTips();
			}else{
				usePopUpBoxTips();
			}
		},
		//密码强度
		/*passwordLevel:function ($input){
			var $passwordLevel=$input.closest('.input-group').find('.passwordLevel');
			function CharMode(pwdLetter){
			    if( pwdLetter >= 48 && pwdLetter <= 57 ){//数字
			        pl_0=1;
			    }else if( pwdLetter >= 65 && pwdLetter <= 90 ){//大写字母
			        pl_A=1;
			    }else if( pwdLetter >= 97 && pwdLetter <= 122 ){//小写字母
			        pl_a=1;
			    }else{//特殊字符
			        pl_$=1;
			    }
			}
			if($passwordLevel.length>0){
				var pl_0=0,pl_A=0,pl_a=0,pl_$=0,pl_l=0;
				var passwordValue=$input.val();
			    for( var i = 0,len=passwordValue.length; i<len ; i++ ){
			        CharMode(passwordValue.charCodeAt(i));
			    }
			    if( passwordValue.length > 4 ){
			    	pl_l=pl_0+pl_A+pl_a+pl_$;
			    }
			    var levelBarWidth='0%';
			    var levelBarColor='#ccc';
			    var levelBarClass='bar';
			    var levelBarTips='';
			    switch(pl_l){
			    	case 0:
			    		levelBarWidth='20%';
			    		levelBarClass='-1';
			    		levelBarTips=UiText.textTranslate('无','form-checkTips');
			    		break;
			    	case 1:
			    		levelBarWidth='40%'
			    		levelBarClass='-2';
			    		levelBarTips=UiText.textTranslate('差','form-checkTips');
			    		break;
			    	case 2:
			    		levelBarWidth='60%'
			    		levelBarClass='-3';
			    		levelBarTips=UiText.textTranslate('中','form-checkTips');
			    		break;
			    	case 3:
			    		levelBarWidth='80%'
			    		levelBarClass='-4';
			    		levelBarTips=UiText.textTranslate('强','form-checkTips');
			    		break;
			    	case 4:
			    		levelBarWidth='100%';
			    		levelBarClass='-5';
			    		levelBarTips=UiText.textTranslate('安全','form-checkTips');
			    		break;
			    }
			    $passwordLevel.children('.bar').attr('class','bar level'+levelBarClass).stop(true,true).animate({'width':levelBarWidth});//.css({'background-color':levelBarColor});
			    $passwordLevel.children('.text').attr('class','text text'+levelBarClass).html(levelBarTips);
			}
		},*/
		inputCompare:function ($input){
			if($input.attr('data-inputcompare')){
				if(FormCheck.inputCheck($input)!=''){
					return false;
				}
			}
			var $targetInput=$('#'+$input.attr('data-inputcompare'));
			var tipsText='';
			if($targetInput.length>0){
				var val1=$targetInput.val();
				var val2=$input.val();
				if(val1!=val2){
					tipsText = 'Confirm do not match'.tr('formCheck');
				}
			}else{
				//tipsText = '不存在可校验对象';
			}
			if(tipsText==''){
				FormCheck.checkResultShow($input,true);
			}else{
				FormCheck.checkResultShow($input,false,tipsText);
			}
			return tipsText;
		},
		childrenInputCheck:function($parent){
			var formCheckResult=true;
			var errorFrom=[];
			$parent.find('[data-formcheck]').each(function(){
				var $input=$(this);
				var inputCheckResult=FormCheck.inputCheck($(this));
				if(!inputCheckResult.status){
					errorFrom.push($input);
					formCheckResult=false;
				};
			});
			$parent.find('[data-inputcompare]').each(function(){
				if(!FormCheck.inputCheck($(this).status)){
					errorFrom.push(this);
					formCheckResult=false;
				};
			});
			if(errorFrom){
				console.log('errorFrom:',errorFrom)
			}
			return formCheckResult;
		},
		/*bind:function(){
			//输入内容校验
			$('body').on('keyup','[data-formcheck]',function(){
				FormCheck.inputCheck($(this));
			})
			$('body').on('keyup','[data-lockbtn]',function(){
				var $this=$(this);
				var checkResultMode=FormCheck.inputCheck($this);
				var $btn=$('#'+$this.attr('data-lockbtn'));
				if(checkResultMode==''){
					$btn.removeAttr('disabled');
				}else{
					$btn.attr('disabled',true);
				}
			})
			//重复校验
			$('body').on('blur','[data-inputcompare]',function(){
				FormCheck.inputCompare($(this));
			})
		}*/


	}

	$('body').on('keyup blur','[data-formcheck]',function(e){
		var $input=$(this);
		var checkonTypeArr=$input.attr('data-checkon') || 'blur';
		checkonTypeArr=checkonTypeArr.split(' ');
		var eventType=e.type;
		if(eventType==='focusout'){
			eventType='blur';
		}
		if(checkonTypeArr.indexOf(eventType)>=0){
			FormCheck.inputCheck($input);
		}

	});
	$('body').on('keyup','[data-lockbtn]',function(){
		var $this=$(this);
		var checkResultMode=FormCheck.inputCheck($this);
		var $btn=$('#'+$this.attr('data-lockbtn'));
		if(checkResultMode==''){
			$btn.removeAttr('disabled');
		}else{
			$btn.attr('disabled',true);
		}
	});
	//重复校验
	$('body').on('blur','[data-inputcompare]',function(){
		FormCheck.inputCompare($(this));
	})

    window.FormCheck = FormCheck;
	return FormCheck;
})
